package com.tsinghuabigdata.edu.ddmath.module.neteaseim.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.nimlib.sdk.chatroom.model.ChatRoomInfo;
import com.netease.nimlib.sdk.document.model.DMData;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.commons.newbieguide.ScreenUtils;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.NimCache;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.base.ui.TFragment;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.ActionTypeEnum;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.DoodleView;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.OnlineStatusObserver;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.SupportActionType;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.Transaction;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.TransactionCenter;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.doodle.action.MyPath;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.helper.ChatRoomMemberCache;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.helper.VideoListener;
import com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.model.Document;
import com.tsinghuabigdata.edu.ddmath.util.DensityUtils;
import com.tsinghuabigdata.edu.ddmath.util.ScreenUtil;

import org.xutils.common.util.DensityUtil;

import java.util.List;
import java.util.Map;

import static android.os.Looper.getMainLooper;

/**
 * Created by hzxuwen on 2016/10/27.
 */

public class ChatRoomRTSFragment extends TFragment implements View.OnClickListener, VideoListener, OnlineStatusObserver, DoodleView.FlipListener {
    private static final String TAG = ChatRoomRTSFragment.class.getSimpleName();
    private View rootView;
    private DoodleView doodleView; // 画板
    // 控制条
    /*private TextView   clearAllBtn;
    private TextView   playbackBtn;
    private TextView   chooseColorBtn;
    private View       joinTipText;
    private ViewGroup  previousPageBtn;
    private ViewGroup  nextPageBtn;
    private ImageView  previousPageImage;
    private ImageView  nextPageImage;
    private TextView   fileBtn;
    private ViewGroup  pageslayout;
    private TextView   pagesText;*/
    // 文档分享
    //    private View       closeFileBtn;
    //    private TextView   fileLoadingText;

    private LinearLayout mLlPlayBack;
    private LinearLayout mLlPaint;
    private TextView tvPenTurn;


    // data
    private String sessionId; // 白板sessionId
    private ChatRoomInfo roomInfo;
    //    private HashMap<Integer, Integer> colorChoosedMap = new HashMap<>();
    //    private HashMap<Integer, Integer> colorMap = new HashMap<>();
    private int choosedColor;

    private int currentPageNum = 0;
    private int totalPageNum = 0;
    private Document document;
    public boolean isFragmentInitDone = false;

    private boolean isFileMode = false; // 是否是文档模式
    private Context mContext;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.chat_room_rts_fragment, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        initData();
        findViews();
        setListener();
        isFragmentInitDone = true;
    }

    public void onCurrent() {
    }

    @Override
    public void onResume() {
        super.onResume();
        doodleView.onResume();
    }

    @Override
    public void onDestroy() {
        if (doodleView != null) {
            doodleView.end();
        }
        registerObservers(false);
        super.onDestroy();
    }

    private void registerObservers(boolean register) {
        ChatRoomMemberCache.getInstance().registerMeetingControlObserver(meetingControlObserver, register);
        TransactionCenter.getInstance().registerOnlineStatusObserver(sessionId, this);
    }

    ChatRoomMemberCache.MeetingControlObserver meetingControlObserver = new ChatRoomMemberCache.MeetingControlObserver() {
        @Override
        public void onAccept(String roomID) {

        }

        @Override
        public void onReject(String roomID) {
            if (!roomID.equals(roomInfo.getRoomId())) {
                return;
            }
            ChatRoomMemberCache.getInstance().setRTSOpen(false);
            initView();
        }

        @Override
        public void onPermissionResponse(String roomId, List<String> accounts) {

        }

        @Override
        public void onSaveMemberPermission(String roomID, List<String> accounts) {

        }

        @Override
        public void onSendMyPermission(String roomID, String toAccount) {

        }

        @Override
        public void onHandsUp(String roomID, String account) {

        }

        @Override
        public void onHandsDown(String roomID, String account) {

        }

        @Override
        public void onStatusNotify(String roomID, List<String> accounts) {
            if (roomInfo == null || TextUtils.isEmpty(roomInfo.getRoomId()) || !roomInfo.getRoomId().equals(roomID)) {
                return;
            }

            // 等权限处理完毕，刷新一下自己的界面
            getHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    initView();
                }
            }, 50);
        }
    };

    private void initData() {

    }

    private void findViews() {
        // 画板
        doodleView = findView(R.id.doodle_view);
        // 控制条
        mLlPlayBack = findView(R.id.ll_play_back);
        mLlPaint = findView(R.id.ll_paint);
        tvPenTurn = findView(R.id.tv_pen_turn);
        mLlPaint.setActivated(true);
        tvPenTurn.setActivated(true);

        ViewGroup.LayoutParams layoutParams = doodleView.getLayoutParams();

        int videoHeight = ScreenUtils.getScreenContentHeight(mContext) - DensityUtils.dp2px(mContext, 20 + 84);

        int maxHeight = ScreenUtils.getScreenContentHeight(mContext) - DensityUtils.dp2px(mContext, 20 + 48 + 20);
        int maxWidth = ScreenUtils.getScreenWidth(mContext) - videoHeight / 2 - DensityUtils.dp2px(mContext, 20 * 3);
        Log.i("Doodle", "maxHeight=" + maxHeight + " maxWidth=" + maxWidth);
        int realWidth;
        int realHeight;
        if (maxWidth * 5 / 7 > maxHeight) {
            realWidth = maxHeight * 7 / 5;
            realHeight = maxHeight;
            layoutParams.width = realWidth;
            int disWidth = maxWidth - realWidth;
            initDoodleViewSize(disWidth / 2, 0);
            Log.i("Doodle", "disWidth=" + disWidth);
        } else {
            realWidth = maxWidth;
            realHeight = maxWidth * 5 / 7;
            layoutParams.height = maxWidth * 5 / 7;
            initDoodleViewSize(0, realHeight / 2);
        }
        Log.i("Doodle", "width=" + layoutParams.width + " height=" + layoutParams.height);
        doodleView.setLayoutParams(layoutParams);
    }

    private void setListener() {
        mLlPlayBack.setOnClickListener(this);
        mLlPaint.setOnClickListener(this);
    }

    public void initRTSView(String sessionId, ChatRoomInfo roomInfo) {
        this.sessionId = sessionId;
        this.roomInfo = roomInfo;
        initView();
        initDoodleView(null);
        registerObservers(true);
    }

    // 初始化是否开启白板
    public void initView() {
//        if (doodleView == null){
//            getHandler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    initView();
//                }
//            },50);
//            return;
//        }
        if (ChatRoomMemberCache.getInstance().isRTSOpen()) {
            doodleView.setEnableView(true);
        } else {
            doodleView.setEnableView(false);
        }
    }


    @Override
    public void onClick(View v) {
        if (v == mLlPaint) {
            //// TODO: 2017/3/21 控制画笔开关
            if (mLlPaint.isActivated()) {
                Log.i(TAG, "isActivated");
                mLlPaint.setActivated(false);
                doodleView.setEnableView(false);
                tvPenTurn.setActivated(false);
            } else {
                Log.i(TAG, "isNOTActivated");
                mLlPaint.setActivated(true);
                doodleView.setEnableView(true);
                tvPenTurn.setActivated(true);
            }
        } else if (v == mLlPlayBack) {
            doodleView.paintBack();
        }
        /*if (v == clearAllBtn) {
            doodleView.clear();
        } else if (v == playbackBtn) {
            doodleView.paintBack();
        } else if (v == fileBtn) {
            //            FileListActivity.startActivityForResult(getContext());
        } else if (v == previousPageBtn) {
            currentPageNum--;
            changePages(document);
        } else if (v == nextPageBtn) {
            currentPageNum++;
            changePages(document);
        } else if (v == closeFileBtn) {
            confirmCloseFileShare();
        } else if (v == fileLoadingText) {
            pageFlip(docTransaction);
        }*/
    }


    /**
     * ***************************** 画板 ***********************************
     */

    private void initDoodleViewSize(final int left, final int top) {
        // adjust paint offset
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                Rect frame = new Rect();
                getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                int statusBarHeight = frame.top;
                Log.i("Doodle", "statusBarHeight =" + statusBarHeight);

                int marginTop = doodleView.getTop();
                Log.i("Doodle", "doodleView marginTop =" + marginTop);

                int marginLeft = doodleView.getLeft();
                Log.i("Doodle", "doodleView marginLeft =" + marginLeft);

                float offsetX = marginLeft + left + DensityUtil.dip2px(20);
                float offsetY = statusBarHeight + marginTop + top + DensityUtil.dip2px(44) + DensityUtil.dip2px(20);

                doodleView.setPaintOffset(offsetX, offsetY);
                Log.i("Doodle", "client1 offsetX = " + offsetX + ", offsetY = " + offsetY);
            }
        }, 50);
    }

    private void initDoodleView(String account) {
        Toast.makeText(getContext(), "init doodle success", Toast.LENGTH_SHORT).show();
        // add support ActionType
        SupportActionType.getInstance().addSupportActionType(ActionTypeEnum.Path.getValue(), MyPath.class);
        doodleView.init(sessionId, account, DoodleView.Mode.BOTH, Color.WHITE, Color.BLACK, getContext(), this);

        doodleView.setPaintSize(3);
        doodleView.setPaintType(ActionTypeEnum.Path.getValue());

        // adjust paint offset
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                Rect frame = new Rect();
                getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                int statusBarHeight = frame.top;
                Log.i("Doodle", "statusBarHeight =" + statusBarHeight);

                int marginTop = doodleView.getTop();
                Log.i("Doodle", "doodleView marginTop =" + marginTop);

                int marginLeft = doodleView.getLeft();
                Log.i("Doodle", "doodleView marginLeft =" + marginLeft);

                float offsetX = marginLeft;
                float offsetY = statusBarHeight + marginTop + ScreenUtil.dip2px(220) + ScreenUtil.dip2px(40);

                doodleView.setPaintOffset(offsetX, offsetY);
                Log.i("Doodle", "client1 offsetX = " + offsetX + ", offsetY = " + offsetY);
            }
        }, 50);
    }

    @Override
    public void onVideoOn(String account) {

    }

    @Override
    public void onVideoOff(String account) {

    }

    @Override
    public void onTabChange(boolean notify) {

    }

    @Override
    public void onKickOutSuccess(String account) {

    }

    @Override
    public void onUserLeave(String account) {

    }

    @Override
    public void onReportSpeaker(Map<String, Integer> map) {

    }

    @Override
    public void onAcceptConfirm() {
        initView();
    }

    /***************************
     * 网络状态
     *********************/

    @Override
    public boolean onNetWorkChange(boolean isCreator) {
        // 断网重连。主播断网重连上来，给观众发自己的同步数据
        // 观众先清空本地
        if (isCreator) {
            doodleView.sendSyncPrepare();
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    doodleView.sendSyncData(null);
                }
            }, 50);
        } else {
            initView();
            doodleView.clearAll();
        }
        return true;
    }

    /********************
     * activity result
     **************/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        //        if (requestCode == FileListActivity.REQUEST_CODE) {
        //            Document doc = (Document) data.getSerializableExtra(FileListActivity.EXTRA_DATA_DOC);
        //            this.document = doc;
        //            enterDocMode(doc);
        //        }
    }

    /*********************
     * 文档共享
     **********************/

    private void enterDocMode(Document document) {
        isFileMode = true;
        updatePagesUI(document, 1);
        //        closeFileBtn.setVisibility(View.VISIBLE);
        Map<Integer, String> pathMap = document.getPathMap();
        if (pathMap == null) {
            return;
        }
        String path = pathMap.get(1);

        // 把正在使用文档，通知给观众
        masterSendFlipData(document, 1);

        // 主播自己界面显示文档信息
        try {
            final Bitmap bitmap = BitmapFactory.decodeFile(path);
            if (bitmap != null) {
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doodleView.setImageView(bitmap);
                    }
                }, 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void updatePagesUI(Document document, int currentPageNum) {
        //        if (isFileMode) {
        //            pageslayout.setVisibility(View.VISIBLE);
        //            if (NimCache.getAccount().equals(roomInfo.getCreator())) {
        //                nextPageBtn.setVisibility(View.VISIBLE);
        //                previousPageBtn.setVisibility(View.VISIBLE);
        //            } else {
        //                nextPageBtn.setVisibility(View.GONE);
        //                previousPageBtn.setVisibility(View.GONE);
        //            }
        //            totalPageNum = document.getDmData().getPageNum();
        //            updatePages(currentPageNum, totalPageNum);
        //            updatePageBtnUI();
        //        } else {
        //            pageslayout.setVisibility(View.GONE);
        //        }
    }

    private void updatePages(int currentNum, int pagesNum) {
        this.currentPageNum = currentNum;
        //        pagesText.setText(String.format("%d/%d", currentNum, pagesNum));
    }

    /**
     * 上下翻页
     */
    private void changePages(Document document) {
        if (currentPageNum < 1) {
            currentPageNum = 1;
            return;
        }
        if (currentPageNum > totalPageNum) {
            currentPageNum = totalPageNum;
            return;
        }
        // 主播翻页
        // 1、自己doodleview显示新的内容
        // 2、发送控制命令给观众
        Bitmap bitmap = BitmapFactory.decodeFile(document.getPathMap().get(currentPageNum));
        doodleView.setImageView(bitmap);
        updatePages(currentPageNum, totalPageNum);
        updatePageBtnUI();
        masterSendFlipData(document, currentPageNum);
    }

    private void updatePageBtnUI() {
        //        if (currentPageNum == 1) {
        //            previousPageImage.setBackgroundResource(R.drawable.ic_previous_page_disable);
        //            previousPageBtn.setEnabled(false);
        //        } else {
        //            previousPageImage.setBackgroundResource(R.drawable.chat_room_previous_page_selector);
        //            previousPageBtn.setEnabled(true);
        //        }
        //        if (currentPageNum == totalPageNum) {
        //            nextPageImage.setBackgroundResource(R.drawable.ic_next_page_disable);
        //            nextPageBtn.setEnabled(false);
        //        } else {
        //            nextPageImage.setBackgroundResource(R.drawable.chat_room_next_page_selector);
        //            nextPageBtn.setEnabled(true);
        //        }
    }

    private void masterSendFlipData(Document document, int currentPageNum) {
        doodleView.clear();
        doodleView.sendFlipData(document.getDmData().getDocId(), currentPageNum, document.getDmData().getPageNum(), 1);
    }

    /************************
     * FlipListener
     *******************/

    @Override
    public void onFlipPage(Transaction transaction) {
        pageFlip(transaction);
    }

    private DMData docData;
    private Transaction docTransaction;

    private void pageFlip(final Transaction transaction) {
        this.docTransaction = transaction;
        if (transaction == null) {
            return;
        }

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showLoadingText();
            }
        });

        // 文档第0页，表示退出文档分享
        if (transaction.getCurrentPageNum() == 0) {
            isFileMode = false;
            closeFileShare();
            hideLoadingText();
            return;
        }
        // 如果文档信息已经下载过了，就不用载了。直接去载翻页图片
        isFileMode = true;

//        if (docData != null && docData.getDocId().equals(transaction.getDocId())) {
//            doDownloadPage(document, transaction.getCurrentPageNum());
//            return;
//        }
//        LogUtil.i(TAG, "doc id:" + transaction.getDocId());
//        DocumentManager.getInstance().querySingleDocumentData(transaction.getDocId(), new RequestCallback<DMData>() {
//            @Override
//            public void onSuccess(DMData dmData) {
//                LogUtil.i(TAG, "query doc success");
//                docData = dmData;
//                document = new Document(dmData, new HashMap<Integer, String>(), FileDownloadStatusEnum.NotDownload);
//                doDownloadPage(document, transaction.getCurrentPageNum());
//            }
//
//            @Override
//            public void onFailed(int i) {
//                LogUtil.i(TAG, "query doc failed, code:" + i);
//                showRetryLoadingText();
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                LogUtil.i(TAG, "query doc exception:" + throwable.toString());
//            }
//        });
    }

    private void doDownloadPage(final Document document, final int currentPage) {
        if (document == null || document.getDmData() == null) {
            return;
        }

        //        final String path = StorageUtil.getWritePath(document.getDmData().getDocName() + currentPage, StorageType.TYPE_FILE);
        //        String url = document.getDmData().getTransCodedUrl(currentPage, DMDocTransQuality.MEDIUM);
        //        Map<Integer, String> pathMap = document.getPathMap();
        //        pathMap.put(currentPage, path);
        //        document.setPathMap(pathMap);
        //        NIMClient.getService(NosService.class).download(url, null, path).setCallback(new RequestCallback() {
        //            @Override
        //            public void onSuccess(Object o) {
        //                hideLoadingText();
        //                Bitmap bitmap = BitmapFactory.decodeFile(path);
        //                LogUtil.i(TAG, "Audience download success, set bitmap:" + bitmap);
        //                doodleView.setImageView(bitmap);
        //                updatePagesUI(document, currentPage);
        //            }
        //
        //            @Override
        //            public void onFailed(int i) {
        //                LogUtil.i(TAG, "Audience download file failed, code:" + i);
        //                showRetryLoadingText();
        //            }
        //
        //            @Override
        //            public void onException(Throwable throwable) {
        //
        //            }
        //        });
    }

    private void confirmCloseFileShare() {
        //        EasyAlertDialogHelper.createOkCancelDiolag(getActivity(), getString(R.string.operation_confirm),
        //                getString(R.string.confirm_to_close_file_share), getString(R.string.close_file_share), getString(R.string.cancel), true,
        //                new EasyAlertDialogHelper.OnDialogActionListener() {
        //                    @Override
        //                    public void doCancelAction() {
        //
        //                    }
        //
        //                    @Override
        //                    public void doOkAction() {
        //                        closeFileShare();
        //                    }
        //                }).show();
    }


    // 退出文档分享
    private void closeFileShare() {
        isFileMode = false;
        doodleView.setBitmap(null);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //                closeFileBtn.setVisibility(View.GONE);
                updatePagesUI(null, -1);
            }
        });
        // 主播退出文档分享，通知观众
        if (NimCache.getAccount().equals(roomInfo.getCreator())) {
            doodleView.clear();
            doodleView.sendFlipData(document.getDmData().getDocId(), 0, 0, 1);
        }
    }

    private void showLoadingText() {
        //        fileLoadingText.setVisibility(View.VISIBLE);
        //        fileLoadingText.setText(R.string.file_loading);
        //        fileLoadingText.setEnabled(false);
    }

    private void hideLoadingText() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //                fileLoadingText.setVisibility(View.GONE);
            }
        });
    }

    private void showRetryLoadingText() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //                fileLoadingText.setVisibility(View.VISIBLE);
                //                fileLoadingText.setText(R.string.failed_to_retry);
                //                fileLoadingText.setEnabled(true);
            }
        });

    }
}
